<template>
  <div>
    <HETTable1
      ref="registryTable"
      :tableData="data.tableData"
      :tHead="data.tHead"
      :headerOperation="!treeStore.isShowStrategyDetail ? data.headerOperation : []"
      :radioBox="treeStore.isShowStrategyDetail ? false : true"
      :total="data.total"
      @changeRadioData="changeRadioData"
      @getTableData="getRegistryList"
    ></HETTable1>
    <DirectoryDialog :urlType="2" @handleDialogButton="choosePath"   :os="treeStore.osType"></DirectoryDialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, markRaw } from "vue";
import { useTreeStore } from "~/utils/pinia/tree.js";
import { ElMessage, ElMessageBox } from "element-plus";
import { Search, Refresh, Delete, Plus } from "@element-plus/icons-vue";
import {
  req_regProtectList,
  req_addRegProtect,
  req_delRegProtect,
} from "~/utils/axios/index.js";
import { eventBus } from "~/utils/common/eventBus.js";
eventBus.on("getStrategyDetail", (detail) => {
  data.tableData = detail.data;
  data.total = detail.total;
});
const treeStore = useTreeStore();
const registryTable = ref();
treeStore.treeLinkedTableRef = registryTable;

const delRegistry = () => {
  ElMessageBox.confirm("确定要删除选中的注册表项？", "删除注册表项", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    center: true,
    closeOnClickModal: false,
  }).then(() => {
    let path = data.selectData.path;
    req_delRegProtect(treeStore.treeDeviceId, { id: data.selectData.id, path }).then(
      (res) => {
        res.data.succeed ? ElMessage.success("删除成功") : ElMessage.error("删除失败");
        registryTable.value.getTableData();
      }
    );
  });
};
const addRegistry = () => {
  treeStore.directoryDialogVModel = true;
};
const choosePath = (ref, path) => {
  if (!path) {
    ElMessage.error("请选择路径");
    return;
  }
  req_addRegProtect(treeStore.treeDeviceId, path).then((res) => {
    res.data.succeed ? ElMessage.success("添加成功") : ElMessage.error("添加失败");
    registryTable.value.getTableData();
  });
};
const data = reactive({
  tHead: [
    {
      label: "注册表项",
      prop: "path",
      isCustom: false,
    },
  ],
  tableData: [],
  selectData: {}, // 单选的数据
  headerOperation: [
    {
      name: "新建规则",
      icon: markRaw(Plus),
      handleClick: addRegistry,
    },
    {
      name: "删除",
      icon: markRaw(Delete),
      handleClick: delRegistry,
    },
  ],
  total: null,
});

const changeRadioData = (selected) => {
  data.selectData = selected;
};
const getRegistryList = async (initTableParams, $filter) => {
  if (!treeStore.treeDeviceId) return;
  registryTable.value.loading();
  initTableParams = { ...initTableParams, type: 0 };
  try {
    const { data: res } = await req_regProtectList(
      treeStore.treeDeviceId,
      initTableParams
    );
    registryTable.value.finish();
    data.tableData = res.list || [];
    data.total = res.total || 0;
    data.selectData = {};
  } catch (err) {
    registryTable.value.finish();
  }
};
</script>

<style lang="scss" scoped></style>
